#define _CRT_SECURE_NO_WARNINGS 1
#include"qqq.h"

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdbool.h>

void QueueInit(Qu* p)
{
	assert(p);
	p->head = p->tail = NULL;
	p->size = 0;
}


void QueuePush(Qu* q, type data)
{
	assert(q);

	QL* newnode = (QL*)malloc(sizeof(QL));
	if (newnode == NULL)
	{
		perror("malloc:");
		exit(-1);
	}
	newnode->data = data;
	newnode->next = NULL;

	if (q->size==0)
	{
		q->head = newnode;
		q->tail = newnode;
		q->size++;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
		q->size++;
	}
}



void QueuePop(Qu* q)
{
	assert(q);
	assert(q->size > 0);
	if (q->size == 1)
	{
		free(q->head);
		q->head = q->tail = NULL;
		q->size--;
	}
	else
	{
		QL* ts = q->head->next;
		free(q->head);
		q->head->next = NULL;
		q->head = ts;
		q->size--;
	}
	


}


type QueueFront(Qu* q)
{
	assert(q);
	assert(q->size > 0);
	return q->head->data;

}

type QueueBack(Qu* q)
{
	assert(q);
	assert(q->size > 0);

	return q->tail->data;
}


int QueueSize(Qu* q)
{
	assert(q);
	return q->size;
}


bool QueueEmpty(Qu* q)
{
	assert(q);

	if (q->size == 0)
	{
		return true;
	}
	return false;
}


void QueueDestroy(Qu* q)
{
	assert(q);

	QL* ts = q->head;
	while (ts)
	{
		QL* next = ts->next;
		free(ts);
		ts = next;
	}
	q->size = 0;
	q->head = q->tail = NULL;
}




